local zhaozhong = fk.CreateSkill{
  name = "rfenghou_3d13j__zhaozhong&",
}
local orig = "rfenghou_3d13j__zhaozhong"

zhaozhong:addEffect("active", {
  mute = true,
  anim_type = "support",
  prompt = function(self, player, cards, targets)
    return targets[1] and "#rfenghou_3d13j__zhaozhong&:" .. targets[1].id or "#rfenghou_3d13j__zhaozhong&0"
  end,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function(p) return p:hasSkill(orig) and p ~= player and Fk.skill_skels[orig]:withinTimesLimit(p, Player.HistoryPhase) end)
  end,
  card_num = 1,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and (Fk:getCardById(to_select).trueName == "jink" or Fk:getCardById(to_select).trueName == "peach")
  end,
  target_num = function(self, player)
    if #table.filter(Fk:currentRoom().alive_players, function(p)
      return p:hasSkill(orig) and p ~= player and Fk.skill_skels[orig]:withinTimesLimit(p, Player.HistoryPhase)
    end) > 1 then
      return 1
    else
      return 0
    end
  end,
  target_filter = function(self, player, to_select, selected)
    if #table.filter(Fk:currentRoom().alive_players, function(p)
      return p:hasSkill(orig) and p ~= player and Fk.skill_skels[orig]:withinTimesLimit(p, Player.HistoryPhase)
    end) > 1 then
      return #selected == 0 and to_select ~= player and to_select:hasSkill(orig) and Fk.skill_skels[orig]:withinTimesLimit(to_select, Player.HistoryPhase)
    else
      return false
    end
  end,
  on_cost = function(self, player, data, extra_data)
    local room = player.room
    local targets = data.tos[1] and data.tos or table.filter(room.alive_players, function(p)
      return p:hasSkill(orig) and p ~= player and Fk.skill_skels[orig]:withinTimesLimit(p, Player.HistoryPhase)
    end)
    local ret = {} ---@type CostData
    if #targets == 1 then
      ret.tos = targets
    else
      ret.tos = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = orig,
        prompt = "#rfenghou_3d13j__zhaozhong-select",
        cancelable = false,
      })
    end
    return ret
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    player:broadcastSkillInvoke(orig)
    target:addSkillUseHistory(orig)
    room:obtainCard(target, effect.cards[1], false, fk.ReasonGive)
    if player.dead or target.dead or not table.find(room:getOtherPlayers(target), function(p) return
      not target:isProhibited(p, Fk:cloneCard("ex_nihilo")) end) then return end
      local tos = room:askToChoosePlayers(player, {
        targets = table.filter(room:getAlivePlayers(false), function(p) return
        not target:isProhibited(p, Fk:cloneCard("ex_nihilo")) end),
        max_num = 1,
        min_num = 1,
        prompt = "#rfenghou_3d13j__zhaozhong_ex_nihilo::"..target.id,
        skill_name = orig,
        cancelable = false})
      if #tos > 0 then
        local tar = tos[1]
        if room:askToSkillInvoke(target, {
          skill_name = orig,
          prompt = "#rfenghou_3d13j__zhaozhong_nil::"..tos[1].id
        }) then
          room:useVirtualCard("ex_nihilo", nil, target, tar, orig, true)
        else
          room:setPlayerMark(target, "@@rfenghou_3d13j__zhaozhong", 1)
        end
      end
  end,
})

zhaozhong:addEffect("invalidity", {
  invalidity_func = function(self, from, skill)
    return skill:isPlayerSkill(from) and from:hasMark("@@rfenghou_3d13j__zhaozhong")
      and skill.name == orig
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d13j__zhaozhong&"] = "召忠",
  [":rfenghou_3d13j__zhaozhong&"] = "出牌阶段限一次，你可以将一张【闪】或【桃】交给徐母，令其对你选择的目标使用【无中生有】。",

  ["#rfenghou_3d13j__zhaozhong&"] = "召忠：将一张【闪】或【桃】交给%src，令其对你选择的目标使用【无中生有】",
  ["#rfenghou_3d13j__zhaozhong&0"] = "召忠：将一张【闪】或【桃】交给徐母，令其对你选择的目标使用【无中生有】",

  ["#rfenghou_3d13j__zhaozhong-select"] = "召忠：请选择发动〖召忠〗的目标角色",

  ["@@rfenghou_3d13j__zhaozhong"] = "召忠 失效",

  ["#rfenghou_3d13j__zhaozhong_ex_nihilo"] = "召忠：请选择一名 %dest 使用无中生有的目标角色",
  ["#rfenghou_3d13j__zhaozhong_nil"] = "召忠：是否对 %dest 使用无中生有？点确认则使用，点取消则此技能失效直至你拼点赢！",
}

return zhaozhong
